﻿using System.Collections.Generic;
using System.Linq;
using Smarly.Forum.Bussines.Model;

namespace Smarly.Web.Areas.StapleForum.Helpers
{
	public static class SectionHelpers
	{
		private static List<SectionModel> _sectionsTree;
		private static List<SectionModel> _flatSections;
		private static readonly object _lockSection = new object();

		public static List<SectionModel> FlatSections
		{
			get
			{
				if (_sectionsTree == null)
				{
					lock (_lockSection)
					{
						if (_sectionsTree == null)
						{
							_sectionsTree = SectionModel.Get(out _flatSections);
						}
					}
				}

				return _flatSections;
			}
		}

		public static SectionModel GetSection(this string hashCode)
		{
			hashCode = (hashCode ?? string.Empty).Trim().ToLower();
			return FlatSections.SingleOrDefault(x => x.HashTag == hashCode);
		}
	}
}